package com.amwybj.dyscript

import android.accessibilityservice.AccessibilityService
import android.accessibilityservice.GestureDescription
import android.graphics.Path
import android.os.Build
import android.os.Handler
import android.util.Log
import android.view.accessibility.AccessibilityEvent
import android.view.accessibility.AccessibilityNodeInfo
import androidx.annotation.RequiresApi
import kotlin.math.log


class MyAccessibilityService : AccessibilityService() {
    private val TAG = "amwybj"
    var mService: MyAccessibilityService? = null

    override fun onServiceConnected() {
        super.onServiceConnected()
        mService = this;
    }

    override fun onAccessibilityEvent(event: AccessibilityEvent?) {
//        val nodeInfo = rootInActiveWindow
//        Log.e("nodeInfo", "$nodeInfo")
        startDy()
        if (event!!.packageName != null && event!!.packageName.equals("com.ss.android.ugc.aweme")) {
//            Log.e(TAG, "onAccessibilityEvent: dy", )
            val nodeInfo = event?.source
            if (nodeInfo.className == null) return
//            findUserAvatar(event)
            getDyAccount(event)

//            Handler().postDelayed(Runnable {
//                kotlin.run {
//                }
//            }, 1000)
        }
    }

    override fun onInterrupt() {
        mService = null
    }

    override fun onDestroy() {
        super.onDestroy()
        mService = null
    }

    /**
     * 辅助功能是否启动
     */
    fun isStart(): Boolean {
        return mService != null
    }

    fun startDy() {
        val packageM = packageManager
        val launcher = packageM.getLaunchIntentForPackage("com.ss.android.ugc.aweme")
        startActivity(launcher)
    }

    fun moveToNext() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            val path = Path()
            path.moveTo(200f, 300f)
            path.lineTo(200f, 900f)
            val builder = GestureDescription.Builder()
            val desc =
                builder.addStroke(GestureDescription.StrokeDescription(path, 100L, 100L)).build()
//            dispatchGesture(desc, DescCallBack(), null)
//            Log.e(TAG, "moveToNext: 11111")
        }
    }

    fun findUserAvatar(event: AccessibilityEvent): AccessibilityNodeInfo? {
        val nodeInfo = event?.source
        var node: AccessibilityNodeInfo? = null
        if (nodeInfo.className == null) return node
        for (i in 0 until nodeInfo.childCount) {
            val nodeChild = nodeInfo.getChild(i)
            if (nodeChild == null) {
                continue
            }
            if (nodeChild.childCount == 0) {
                continue
            }
            if (nodeChild.viewIdResourceName == null) {
                continue
            }
            for (j in 0 until nodeChild.childCount) {
                val nodeChildToChild = nodeChild.getChild(j)
//                Log.e(TAG, "nodeChildToChild:${nodeChildToChild.childCount} ")

                if (nodeChildToChild.childCount == 0) {
                    continue
                }
                if (nodeChildToChild.viewIdResourceName == null) {
                    continue
                }
                for (k in 0 until nodeChildToChild.childCount) {
                    val nodeChildToChildToChild = nodeChildToChild.getChild(k)
                    if (nodeChildToChildToChild.viewIdResourceName == "com.ss.android.ugc.aweme:id/user_avatar") {
                        node = nodeChildToChildToChild
                        node.performAction(AccessibilityNodeInfo.ACTION_CLICK)
//                        Log.e(TAG, "findUserAvatar: $node", )
                        break
                    }
                }
            }
        }
        return node
    }

    fun getDyAccount(event: AccessibilityEvent) {
//        Log.i(TAG, "getDyAccount: 1111111")
        val nodeInfo = event?.source
//        Log.e(TAG, "nodeInfo: $nodeInfo", )
        if (nodeInfo.className == null) return
        for (i in 0 until nodeInfo.childCount) {
            val nodeChild = nodeInfo.getChild(i)?:continue
            if (nodeChild.className == null) {
                continue
            }
            if (nodeChild.childCount == 0) {
                continue
            }
            for (k in 0 until nodeChild.childCount) {
                val nodeChildToChild = nodeChild.getChild(k) ?: continue
                if (nodeChildToChild.className == null) {
                    continue
                }
                if (nodeChildToChild.childCount == 0) {
                    continue
                }
//                Log.e(TAG, "getDyAccount: ${nodeChildToChild.viewIdResourceName}",)
//                Log.e(TAG, "getDyAccount: ${nodeChildToChild.className}",)
//                Log.e(TAG, "getDyAccount: ${nodeChildToChild.text}",)
//                Log.e(TAG, "getDyAccount: ${nodeChildToChild.isClickable}",)
                for (j in 0 until nodeChildToChild.childCount) {
                    val nodeLaster = nodeChildToChild.getChild(j)
                    for (n in 0 until nodeLaster.childCount) {
                        val nodeLaster1 = nodeLaster.getChild(n)
                        for (m in 0 until nodeLaster1.childCount) {
                            val nodeLaster2 = nodeLaster1.getChild(m)
//                            Log.e(TAG, "getDyAccount: ${nodeLaster2.childCount}", )
                            for (l in 0 until nodeLaster1.childCount) {
                                val nodeLaster3 = nodeLaster2.getChild(l) ?: continue
                                if(nodeLaster3.viewIdResourceName==null){
                                    continue
                                }
                                Log.e(TAG, "getDyAccount: ${nodeLaster3.viewIdResourceName}")
                                Log.e(TAG, "getDyAccount: ${nodeLaster3.className}")
                                Log.e(TAG, "getDyAccount: ${nodeLaster3.text}")
                                Log.e(TAG, "getDyAccount: ${nodeLaster3.isClickable}")
                            }

//                            Log.e(TAG, "getDyAccount: ${nodeLaster2.viewIdResourceName}")
//                            Log.e(TAG, "getDyAccount: ${nodeLaster2.className}")
//                            Log.e(TAG, "getDyAccount: ${nodeLaster2.text}")
//                            Log.e(TAG, "getDyAccount: ${nodeLaster2.isClickable}")
                        }

//                            Log.e(TAG, "getDyAccount: ${nodeLaster.getChild(n).childCount}", )
//                        Log.e(TAG, "getDyAccount: ${nodeLaster.getChild(n).text}", )
//                        Log.e(TAG, "getDyAccount: ${nodeLaster.getChild(n).viewIdResourceName}", )
                    }

//                    if(nodeLaster.viewIdResourceName=="com.ss.android.ugc.aweme:id/title"){
////                        nodeLaster.performAction(AccessibilityNodeInfo.ACTION_CLICK)
//                    }
//                    Log.e(TAG, "getDyAccount: ${nodeLaster.className}")
//                    Log.e(TAG, "getDyAccount: ${nodeLaster.viewIdResourceName}")
//                    Log.e(TAG, "getDyAccount: ${nodeLaster.text}")
//                    Log.e(TAG, "getDyAccount: ${nodeLaster.isClickable}")
//                    Log.e(TAG, "getDyAccount: ${nodeLaster.describeContents()}")
                }

//                Log.e(TAG, "getDyAccount: $nodeChild", )
            }

        }


        @RequiresApi(Build.VERSION_CODES.N)
        class DescCallBack : GestureResultCallback() {
            override fun onCompleted(gestureDescription: GestureDescription?) {
                super.onCompleted(gestureDescription)
            }

            override fun onCancelled(gestureDescription: GestureDescription?) {
                super.onCancelled(gestureDescription)
            }
        }

    }
}